package core.rdd.广播变量;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *对于RDD而言，foreach是执行在executer端的，并没有在Driver端执行，数据也不会返回给Driver端
 */


public class Spark01_broadcast {
    public static void main(String[] args) {
        /**
         *flatMap 是将多集合转换成一个
         */
        // 配置SparkConf指向你的Spark master URL
        SparkConf conf = new SparkConf()
                .setAppName("Spark01_broadcast") // 应用名称
                .setMaster("local[*]"); // 替换成你的master地址
        JavaSparkContext sc = new JavaSparkContext(conf);
        // 创建JavaSparkContext，它是与集群交互的主要入口点
        try {

            List<Integer> asList = Arrays.asList(
                    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            );

            JavaRDD<Integer> parallelize = sc.parallelize(asList);


            Integer reduce = parallelize.reduce(new Function2<Integer, Integer, Integer>() {
                @Override
                public Integer call(Integer v1, Integer v2) throws Exception {
                    return v1+v2;
                }
            });


            AtomicInteger sum= new AtomicInteger();
            parallelize.foreach(integer -> {
                sum.set(integer + sum.get());
            });



            System.out.println(sum);
        } finally {
            sc.close();
        }
    }
}


